<script type="text/javascript" src="<?php echo base_url('assets/tiny_mce/tiny_mce.js');?>"></script>
<script type="text/javascript" src="<?php echo base_url('assets/js/jQuery/mudim.packed.js');?>"></script>
<script type="text/javascript" src="<?php echo base_url('assets/js/jQuery/editpost.js');?>"></script>
<script type="text/javascript" src="<?php echo base_url('assets/js/tooltip/form-field-tooltip.js');?>"></script>
<script type="text/javascript" src="<?php echo base_url('assets/js/tooltip/rounded-corners.js');?>"></script>
<script type="text/javascript" src="<?php echo base_url('assets/js/ajaxupload.js');?>"></script>
<link rel="stylesheet" href="<?php echo base_url('assets/js/tooltip/form-field-tooltip.css');?>" type="text/css">
    <div class="width690 post">
        <h3 class="title-post"><strong> Đăng tin rao vặt</strong></h3>
        <?php if ( validation_errors() ){ ?>

        <div class="form-error-container">
            <h2 class="form-error">Thông báo lỗi</h2>
            <h3>Vui lòng kiểm tra lại theo các thông báo sau</h3>
            <ul>
                <?php echo validation_errors(); ?>
            </ul>
        </div>
    <?php } ?>
        <form action="<?php echo site_url('dang-tin-rao-vat');?>" method="post"  id="postForm" name="postForm">

        <table cellpadding="0" cellspacing="0" width="100%" border="0">
            <tr>
                <td colspan="3" style="padding-top:5px;padding-bottom:5px;">
                     <div><span>Chọn kiểu gõ</span>
                            <input type="radio" name="typing_method"  value="4" /><span style="padding: 2px;">Tự động</span>
                            <input type="radio" name="typing_method" checked="checked" value="2" /><span style="padding: 2px;">Telex</span>
                            <input type="radio" name="typing_method" value="1" /><span style="padding: 2px;">VNI</span>
                            <input type="radio" name="typing_method" value="0"  /><span style="padding: 2px;">Tắt</span>
                        </div>
                     </div>
                </td>
            </tr>
            <tr>

                <td colspan="3">
                    <div style="padding-right: 20px;width: 70%;"><input type="text" name="name" style="width: 550px;"  class="input" id="name" value="<?php echo @$post['name']; ?>" tooltiptext="Vui lòng nhập tiếng Việt có dấu, không dùng chữ IN HOA"></div></td>
            </tr>
            <tr>
                <td colspan="3" class="editor">
                    <textarea  class="simple-editor input" name="content" id="post_content"><?php echo @$post['content']; ?></textarea>
                </td>
            </tr>
            <tr><td colspan="3" style="padding:0px;height: 1px;">&nbsp;</td></tr>
             <tr>
                <td>Chuyên mục <span class="clred">(*)</span></td>
                <td>
                    <select name="category" id="category" class="select" tooltiptext="Bạn phải chọn chuyên mục phù hợp với tin đăng. Nếu sai chuyên mục, tin của bạn sẽ bị xóa.">
                        <option value="">Chọn chuyên mục</option>
                        <?php
                            foreach($this->all_cats as $item){
                                if($item['parent']==0){
                                echo '<optgroup label="'.$item['name'].'">';
                                    foreach($this->all_cats as $val){
                                        if($item['id']==$val['parent']){
                                            if($val['id']==$post['sub_cat_id']){
                                                echo '<option value="'.$val['id'].'" selected="selected">'.$val['name'].'</option>';
                                            }else{
                                                echo '<option value="'.$val['id'].'">'.$val['name'].'</option>';
                                            }
                                        }
                                    }

                                echo '</optgroup>';
                                }
                            }
                        ?>
                    </select>
                </td>
                 <td rowspan="10" align="top" valign="top">
                     <div class="postItemOther-right">
                     <div style="padding-bottom: 7px;">Chọn ảnh để upload<span>(gif, png, jpg, jpeg)  <span class="red">2MB</span></div>
                     <div class="postItemOtherAvatar">
                         <div class="post-upload">
                             <div></div>
                             <div id="loader_overlay" class="loader_overlay_BG"></div>
                             <div id="loader" class="loading"></div>
                             <div id="msg"></div>
                             <div class="post-upload-items uploaded" id="upload-div">
                                 <img class="thumb312" src="<?php echo base_url(@$post['image']) ?>" onerror="this.src='<?php echo base_url('medias/images/post_item_no_image.jpg') ?>'">
                             </div>
                         </div>
                         <div class="avatarTool">
                             <a href="javascript:void(0)">Gallery</a>
                             <a href="javascript:void(0)" id="upload-button" class="bottom">Up ảnh đại diện</a>
                         </div>
                     </div>
                         </div>
                 </td>
            </tr>

            <tr>
                <td>Chủng loại</td>
                <td>
                    <?php
                    if(@$post['id']){
                    echo '<select name="childen" id="childen" class="select">';
                        echo '<option value="">Chọn chủng loại</option>';
                            $child = get_category($post['cat_id']);
                            foreach($child as $key=>$val){
                                if($val['id']==$post['child_cat_id']){
                                    echo '<option value="'.$val["id"].'">'.$val["name"].'</option>';
                                }else {
                                    echo '<option value="'.$val["id"].'" selected="selected">'.$val["name"].'</option>';
                                }
                            }
                            echo '</select>';
                        }
                        else {
                            echo '<select name="childen" id="childen" class="select" disabled="disabled">';
                            echo '<option value="">Chọn chủng loại</option>';
                            echo '</select>';
                        }
                        ?>
                </td>
            </tr>

             <tr>
                <td>Nơi bán <span class="clred">(*)</span></td>
                <td>
                    <select name="provinder" id="provinder" class="select">
                        <option value="">Chọn nơi đăng</option>
                        <?php
                            foreach($this->all_provinder as $val){
                                if($post['provinder_id']==$val['id']){
                                    echo '<option value="'.$val['id'].'" selected="selected">'.$val['name'].'</option>';
                                }else{
                                    echo '<option value="'.$val['id'].'">'.$val['name'].'</option>';
                                }
                            }
                        ?>
                    </select>
                </td>
            </tr>
            <tr>
                <td>Giá bán (vnđ) <span class="clred">(*)</span></td>
                <td>

                    <input type="text" name="promotion" value="<?php echo @$post['promotion']; ?>" id="promotion" class="input">
                </td>
            </tr>
             <tr>
                <td>Giá thị trường (vnđ) </td>
                <td>
                    <input type="text" name="price" value="<?php echo @$post['price']; ?>" id="price" class="input">
                </td>
             </tr>

           <tr><td style="padding:0px;height:10px;"></td></tr>
           <tr>
           <td ></td>
            <td>
                <input type="hidden" id="images" name="image" value="<?php echo @$post['image']; ?>">
                <input type="hidden" id="post_id" name="post_id" value="<?php echo @$post['id']; ?>">
                <input  type="submit" id="submit" class="buttonDisabled btn-small-blue" name="submit" value="Đăng tin">
            </td>
           </tr>
           <tr><td colspan="2">&nbsp;</td></tr>
        </table>
        </form>
    </div>
    <div class="width300">111</div>


<script type="text/javascript">
    var tooltipObj = new DHTMLgoodies_formTooltip();
    tooltipObj.setTooltipPosition('right');
    tooltipObj.setPageBgColor('#EEEEEE');
    tooltipObj.setTooltipCornerSize(10);
    tooltipObj.initFormFieldTooltip();
    jQuery(document).ready(function(){
        //load image da upload truoc day, tu cookie
        loadUploadedImages();
        jQuery('#loader').hide();
        new AjaxUpload('upload-button', {
            action: '<?php echo base_url("upload/index");?>',
            responseType: 'json',
            onChange : function(file , ext) {
                //alert("Please select a directory, then Upload");
                return true;
            },
            onSubmit : function(file , ext){
                var images = Get_Cookie( 'postimages' ) ;
                if(images!=null){
                    images = images.split(";");
                    //alert(images.length);
                    if(images.length>10){
                        alert('Bạn chỉ được phép upload 10 ảnh/1 tin');
                        return false;
                    }
                }
                // Allow only images. You should add security check on the server-side.
                if (ext && /^(gif|jpg|jpeg|png)$/i.test(ext)){
                    /* Change status text */
                    //$('#upload-div .text').text('Uploading ' + file);
                    jQuery('#loader_overlay').show();
                    jQuery('#loader').show();
                    //this.disable();
                } else {
                    // extension is not allowed
                    jQuery('#msg').text('Bạn chỉ được phép upload file ảnh gif,jpg,png,jpeg');
                    // cancel upload
                    return false;
                }
            },
            onComplete : function(file, response){

                jQuery('#msg').hide();
                jQuery('#loader').hide();
                jQuery('#loader_overlay').hide();
                var randomnumber= "thumb_" + Math.floor(Math.random()*11);
                if (response && /^(success)/i.test(response.status)) {
                    $('#images').val(response.file_url);
                    jQuery('#upload-div').html('<img class="thumb312" src="'+base_url+response.file_url+'">'+'[<span class="delete-image"><a title="Xóa ảnh này!" class="gallery" onclick="return del_image(\''+response.file_url+'\', \''+ randomnumber +'\');" href="#">Xóa</a></span>]');
                    // Chen anh vao cookie
                    if ( Get_Cookie( 'postimages' ) ){
                        del(Get_Cookie( 'postimages' ));
                        Set_Cookie( 'postimages', response.file_url, '', '/', '', '' );
                    }
                    else{
                        response.file_url = response.file_url;
                        Set_Cookie( 'postimages', response.file_url, '', '/', '', '' );
                    }
                } else {
                    alert('Lỗi tải ảnh '+file+'! \n'+ response.issue);
                    $('#upload-div .text').html('Upload FAILED');
                }
            }
        });
    });

    function loadUploadedImages(){


        if ( Get_Cookie( 'postimages' ) ) {
            var images = Get_Cookie( 'postimages' ) ;
            if(images!=null){
                $('#images').val(images);
                $('#upload-div').html(

                        '<img class="thumb312" src="'+base_url+images+'">'+
                                '[<span class="delete-image"><a title="Xóa ảnh này!" class="gallery" onclick="return del_image(\''+images+'\');" href="javascript:void(0)">Xóa</a></span>]'
                );

            }
           // $('a.thumbnail').lightBox();
        }
    }

    // Xoa anh tren server
    function del_image(url){
        if ( confirm("Bạn có muốn xóa ảnh này?") ) {
            $.post("<?php echo site_url('/post/deleteimage');?>", { 'imgurl': url}, function(data) {
                // Xoa anh khoi cookie


                    $('#upload-div img').attr('src',''+base_url+'medias/images/post_item_no_image.jpg');
                    if( Get_Cookie("postimages") ) {
                        $('#images').val();
                        Set_Cookie( 'postimages', "", '', '/', '', '' );
                    }

            } );

        }
        return false;
    }
    function del(url){
            $.post("<?php echo site_url('/post/deleteimage');?>", { 'imgurl': url}, function(data) { } );

       return false;
    }
</script>

